#include <stdio.h>
#include <omp.h>
#include <time.h>
#include <stdlib.h>
//编译指令
//gcc -std=c99 -fopenmp 9Barrier.c -o cmake-build/9Barrier && export OMP_NUM_THREADS=512 && time ./cmake-build/9Barrier

void print_time(int TID,char*comment)
{
    time_t tp;
    time(&tp);
    printf("Thread %d %s barrier at %s",TID,comment,ctime(&tp));
}
int main()
{
    int tid;
#pragma omp parallel private(tid)  default(none)
    {
        tid=omp_get_thread_num();
        if(tid<omp_get_num_threads()/2)
            system("sleep 5"); //当前进程睡眠五秒钟
        print_time(tid,"before");
        #pragma omp barrier                  //所有进程同步
        print_time(tid,"after");
    }
    return 0;
}
